﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using bdnt_pets_system_Entity;
using System.Windows.Data;
using bdnt_pets_system.Resources;

namespace bdnt_pets_system.PageViews.SystemViews
{
    public partial class NewDictInfoControl : BaseUserControl
    {
        SystemServiceReference.SystemServiceClient sysClient;
        public NewDictInfoControl()
        {
            InitializeComponent();
        }

        public NewDictInfoControl(Tbl_DictInfo cuTbl_DictInfo)
        {
            InitializeComponent();

            if (cuTbl_DictInfo.Id == 0)
                tbPageIitle.Text = MyStrings.xjsjzd;//"新建数据字典";
            //tbPageIitle.SetBinding(TextBlock.TextProperty, new Binding("Strings.xjcd") { Source = CuLc });
            else tbPageIitle.Text = MyStrings.xgsjzd;// "修改数据字典";
            //tbPageIitle.SetBinding(TextBlock.TextProperty, new Binding("Strings.xgcd") { Source = CuLc });

            sysClient = new SystemServiceReference.SystemServiceClient();
            CuTbl_DictInfo = cuTbl_DictInfo;
            BingData();
            InitEvnet();
        }

        public Tbl_DictInfo CuTbl_DictInfo
        {
            get { return this.DataContext as Tbl_DictInfo; }
            set { this.DataContext = value; }
        }

        void BingData()
        {
            tbEName.SetBinding(TextBox.TextProperty, new Binding("en_DictName")
            {
                Mode = BindingMode.TwoWay,
                NotifyOnValidationError = true,
                ValidatesOnExceptions = true
            });
            tbZName.SetBinding(TextBox.TextProperty, new Binding("DictName")
            {
                Mode = BindingMode.TwoWay,
                NotifyOnValidationError = true,
                ValidatesOnExceptions = true
            });
            tbDValue.SetBinding(TextBox.TextProperty, new Binding("DictValue")
            {
                Mode = BindingMode.TwoWay,
                NotifyOnValidationError = true,
                ValidatesOnExceptions = true
            });

            tbRemark.SetBinding(TextBox.TextProperty, new Binding("Remark") { Mode = BindingMode.TwoWay });
            tbPId.SetBinding(TextBlock.TextProperty, new Binding("PName") { Mode = BindingMode.TwoWay });
            tbSq.SetBinding(TextBox.TextProperty, new Binding("Sequence")
            {
                Mode = BindingMode.TwoWay,
                NotifyOnValidationError = true,
                ValidatesOnExceptions = true
            });
            CuTbl_DictInfo.DictType = 0;
        }

        void InitEvnet()
        {
            btnOk.Click += new RoutedEventHandler(btnOk_Click);
            btnCancel.Click += new RoutedEventHandler(btnCancel_Click);
            sysClient.SaveDictObjCompleted += new EventHandler<SystemServiceReference.SaveDictObjCompletedEventArgs>(sysClient_SaveDictObjCompleted);

        }

        void sysClient_SaveDictObjCompleted(object sender, SystemServiceReference.SaveDictObjCompletedEventArgs e)
        {
            if (e.Result)
                App.ToPage(new DictInfoManagerControl()
                {
                    selectId = CuTbl_DictInfo.Id > 0 ? CuTbl_DictInfo.Id : CuTbl_DictInfo.PId,
                    isExpSelect = CuTbl_DictInfo.Id > 0 ? false : true
                });
            else
                errorMsg.SetBinding(TextBlock.TextProperty, new Binding("Strings.bcsb") { Source = CuLc });
        }

        void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            App.ToPage(new DictInfoManagerControl()
            {
                selectId = CuTbl_DictInfo.Id > 0 ? CuTbl_DictInfo.Id : CuTbl_DictInfo.PId,
                isExpSelect = CuTbl_DictInfo.Id > 0 ? false : true
            });
        }
        /// <summary>
        /// 字段验证方法
        /// </summary>
        void BindgExpression()
        {
            tbEName.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            tbZName.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            tbSq.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            tbDValue.GetBindingExpression(TextBox.TextProperty).UpdateSource();
        }

        void btnOk_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                BindgExpression();
                if (vs.Errors.Count > 0)
                {
                    errorMsg.Text = MyStrings.sjtxbq;// "数据填写不全！";
                    return;
                }

                sysClient.SaveDictObjAsync(CuTbl_DictInfo);
            }
            catch (Exception)
            {

            }
        }

    }
}
